//
//  SecondViewController.m
//  testFullScreen
//
//  Created by Juvid on 2017/6/15.
//  Copyright © 2017年 Juvid. All rights reserved.
//

#import "SecondViewController.h"

@interface SecondViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *ju_imageView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *ju_layW;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *ju_layH;
@property(nonatomic) CGFloat maximumZoomScale;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _ju_imageView.image=[UIImage imageNamed:@"timg.jpeg"];
    [self juSetImageSize:[[UIScreen mainScreen] bounds].size];
    // Do any additional setup after loading the view.
}

- (BOOL)shouldAutorotate{
    return YES;
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

//- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
//    ;
//    ;
//}

-(void)juSetImageSize:(CGSize)size{
    CGSize  imgSize = _ju_imageView.image.size;
    CGFloat multiple=size.width/size.height;
    CGFloat imgMultiple=imgSize.width/imgSize.height;
    //    第一种方法
    //    if (size.width>size.height) {///< 横屏
    //        if (multiple>=imgMultiple) {///< 高全屏
    //             _ju_layH.constant=size.height;
    //             _ju_layW.constant=imgMultiple*size.height;
    //        }else{///< 宽全屏
    //            _ju_layW.constant=size.width;
    //            _ju_layH.constant=size.width/imgMultiple;
    //        }
    //
    //    }else{///< 竖屏
    if (multiple>=imgMultiple) {///< 高全屏
        _ju_layH.constant=size.height;
        _ju_layW.constant=imgMultiple*size.height;
    }else{////< 宽全屏
        _ju_layW.constant=size.width;
        _ju_layH.constant=size.width/imgMultiple;
    }
    //    }
    
    //    第二种方法
    /* float scaleX = size.width/imgSize.width;
     float scaleY = size.height/imgSize.height;
     CGRect scaleOriginRect;
     //倍数小的，先到边缘
     if (scaleX > scaleY)
     {
     //Y方向先到边缘
     float imgViewWidth = imgSize.width*scaleY;
     self.maximumZoomScale =MAX(2.5, size.width/imgViewWidth) ;
     scaleOriginRect = (CGRect){size.width/2-imgViewWidth/2,0,imgViewWidth,size.height};
     }
     else
     {
     //X先到边缘
     float imgViewHeight = imgSize.height*scaleX;
     self.maximumZoomScale =MAX(2.5, size.height/imgViewHeight) ;
     scaleOriginRect = (CGRect){0,size.height/2-imgViewHeight/2,size.width,imgViewHeight};
     }
     _ju_layW.constant=scaleOriginRect.size.width;
     _ju_layH.constant=scaleOriginRect.size.height;*/
}
-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
    [self juSetImageSize:size];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
//    return UIInterfaceOrientationMaskAll;
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
